package cn.the.angry.sprite;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public abstract class BaseSprite {
	protected boolean visible = true; // is sprite visible
	protected boolean active = false; // is sprite updatable
	protected float x;
	protected float y;
	protected float angle;
	protected BufferedImage[] frames;
	private BufferedImage curFrame;
	private int curNum;
	private final int frameSize;
	protected final int fWidth;
	protected final int fHeight;
	protected int bufX;
	protected int bufY;
	protected BufferedImage bufImg;
	protected int updateTime = 1000; //动画的更新时间间隔，单位为毫秒
	protected long lastUpdateTime;

	public BaseSprite(final BufferedImage[] _frames){
		frames = _frames;
		frameSize = frames.length;
		curFrame = frames[0];
		fHeight = (curFrame.getHeight(null));
		fWidth = (curFrame.getWidth(null));
	}
	
	public void draw(Graphics2D g){
		update();
	}

	public void update(){
		if(active){
			long curNanoTime = System.nanoTime();
			if(lastUpdateTime==0){
				lastUpdateTime = curNanoTime;
			}
			if (curNanoTime-lastUpdateTime > updateTime*1000000) {
				getNextFrame();
				lastUpdateTime = System.nanoTime();
			}
		}
	}
	
	public void getNextFrame(){
		curFrame = ++curNum >= frameSize ? frames[curNum = 0]: frames[curNum];
	}
	
	public void setAttributes(float _x, float _y, float _angle){
		x = _x;
		y = _y;
		angle = _angle;
	}
	
	public float getX()
	{
		return x;
	}
	
	public float getY()
	{
		return y;
	}
	
	public float getAngle()
	{
		return angle;
	}
	
	public int getWidth()
	{
		return fWidth;
	}

	public int getHeight()
	{
		return fHeight;
	}
	
	public int getCenterX()
	{
		return (int) (x+fWidth/2);
	}
	
	public int getCenterY()
	{
		return (int) y+fHeight/2;
	}
	
	public void setCenterX(float x)
	{
		this.x = x-fWidth/2;
	}
	
	public void setCenterY(float y)
	{
		this.y = y-fHeight/2;
	}

	public void setX(float x)
	{
		this.x = x;
	}

	public void setY(float y)
	{
		this.y = y;
	}
	
	public boolean isCoordinateOnSprite(float x, float y)
	{
		return
			x >= this.x && x<= this.x+this.fWidth &&
			y >= this.y && y<= this.y+this.fHeight;
	}

	public void setAngle(float angle)
	{
		this.angle = angle;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean b) {
		visible = b;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean b) {
		active = b;
	}

	// suspend the sprite
	public void suspend() {
		setVisible(false);
		setActive(false);
	}

	// restore the sprite
	public void restore() {
		setVisible(true);
		setActive(true);
	}

	public void setCurFrame(BufferedImage curFrame) {
		this.curFrame = curFrame;
	}

	public BufferedImage getCurFrame() {
		return curFrame;
	}
	
	public void setUpdateTime(int _updateTime){
		updateTime = _updateTime;
	}
	
	public int getFrameSize(){
		return frameSize;
	}
}
